package com.xsy.carRental.service.serviceImpl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xsy.carRental.constant.MessageConstant;
import com.xsy.carRental.dto.RentalAddDTO;
import com.xsy.carRental.dto.RentalPageQueryDTO;
import com.xsy.carRental.dto.RentalSearchQueryDTO;
import com.xsy.carRental.dto.RentalUpdateDTO;
import com.xsy.carRental.entity.Rental;
import com.xsy.carRental.entity.Vehicle;
import com.xsy.carRental.mapper.RentalMapper;
import com.xsy.carRental.mapper.VehicleMapper;
import com.xsy.carRental.result.PageResult;
import com.xsy.carRental.service.RentalService;
import com.xsy.carRental.utils.DateUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class RentalServiceImpl implements RentalService {


    @Autowired
    private RentalMapper rentalMapper;
    @Autowired
    private VehicleMapper vehicleMapper;

    /**
     * 创建租车订单
     *
     * @param rentalAddDTO
     */
    public void add(RentalAddDTO rentalAddDTO) {

        Rental rental = new Rental();
        BeanUtils.copyProperties(rentalAddDTO, rental);
        rental.setCreatedAt(LocalDateTime.now());

        rental.setStatus(MessageConstant.RENTAL_UNPLAYED);
        rental.setLateFee(MessageConstant.RENTALLATEFEE_DEFAULT);

        Vehicle vehicleById = vehicleMapper.getVehicleById(rental.getVehicleId());
        vehicleById.setCreatedAt(LocalDateTime.now());
        vehicleMapper.update(vehicleById);
        // TODO 这里一但给车辆的状态修改  例如两周后被预定 车辆状态改变  但在此之前还能再租赁
        int dailyRate = vehicleById.getDailyRate();

        Vehicle vehicle = new Vehicle();
        vehicle.setStatus(MessageConstant.VEHICLESTATUS_RENTED);
        vehicle.setVehicleId(vehicleById.getVehicleId());
        vehicleMapper.update(vehicle);
        int days = (int) DateUtils.calculateDaysBetween(rentalAddDTO.getStartDate(), rentalAddDTO.getEndDate());
        days++;
        rental.setTotalCost(dailyRate * days);

        rentalMapper.add(rental);
    }

    /**
     * 根据订单id查询订单详细信息
     *
     * @param rentalId
     * @return
     */
    public Rental getRentalById(int rentalId) {
        Rental rental = rentalMapper.getRentalById(rentalId);
        return rental;
    }

    /**
     * 修改订单详细信息
     *
     * @param rentalUpdateDTO
     * @return
     */
    public void update(RentalUpdateDTO rentalUpdateDTO) {

        Rental rental = new Rental();
        BeanUtils.copyProperties(rentalUpdateDTO, rental);
        rental.setUpdatedAt(LocalDateTime.now());

        Vehicle vehicleById = vehicleMapper.getVehicleById(rental.getVehicleId());
        vehicleMapper.update(vehicleById);
        int dailyRate = vehicleById.getDailyRate();
        int days = (int) DateUtils.calculateDaysBetween(rentalUpdateDTO.getStartDate(), rentalUpdateDTO.getEndDate());
        days++;
        rental.setTotalCost(dailyRate * days);
        rentalMapper.update(rental);

    }

    /**
     * 根据订单id删除订单
     *
     * @param rentalId
     */
    public void delete(Integer rentalId) {
        rentalMapper.delete(rentalId);
    }

    /**
     * 分页查询订单
     *
     * @param rentalPageQueryDTO
     * @return
     */
    public PageResult pageQuery(RentalPageQueryDTO rentalPageQueryDTO) {
        PageHelper.startPage(rentalPageQueryDTO.getPage(), rentalPageQueryDTO.getPageSize());
        Page<Rental> page = rentalMapper.pageQuery(rentalPageQueryDTO);

        long total = page.getTotal();
        List<Rental> rentals = page.getResult();
        return new PageResult(total, rentals);
    }

    /**
     * 根据三个可选条件查询订单
     *
     * @param searchQueryDTO
     * @return
     */
    public PageResult SearchQuery(RentalSearchQueryDTO searchQueryDTO) {
        PageHelper.startPage(searchQueryDTO.getPage(), searchQueryDTO.getPageSize());
        Page<Rental> page = rentalMapper.SearchQuery(searchQueryDTO);

        long total = page.getTotal();
        List<Rental> rentals = page.getResult();
        return new PageResult(total, rentals);
    }
}
